package com.cartel.genius.services.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.cartel.genius.dao.exceptions.CategoryExistsException;
import com.cartel.genius.dao.exceptions.CategoryNotFoundException;
import com.cartel.genius.dao.exceptions.CategoryParentNotFoundException;
import com.cartel.genius.dto.CategoryTO;
import com.cartel.genius.services.CategoryService;

/**
 * Webservice delegate class for category operations
 * 
 * @author Naresh Reddy
 */
@Path("/")
@Produces({ MediaType.APPLICATION_XML })
@WebService(targetNamespace = "http://com.cartel.genius.services.CategoryService")
public class CategoryServiceDelegate implements CategoryService {

	private CategoryService categoryService;

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@POST
	@Path("/add/root/{categoryName}")
	@WebMethod(operationName = "addRootCategory")
	public CategoryTO addRootCategory(
			@PathParam("categoryName") @WebParam(name = "categoryName") String categoryName)
			throws CategoryExistsException {
		return categoryService.addRootCategory(categoryName);
	}

	@POST
	@Path("/add/categoryName/{categoryName}/parentId/{parentCategoryId}")
	@WebMethod(operationName = "addCategoryWithParentId")
	public CategoryTO addCategory(
			@PathParam("categoryName") @WebParam(name = "categoryName") String categoryName,
			@PathParam("parentCategoryId") @WebParam(name = "parentCategoryId") Integer parentCategoryId)
			throws CategoryExistsException, CategoryParentNotFoundException {
		return categoryService.addCategory(categoryName, parentCategoryId);
	}

	@POST
	@Path("/add/categoryName/{categoryName}/parentName/{parentCategoryName}")
	@WebMethod(operationName = "addCategoryWithParentName")
	public CategoryTO addCategory(
			@PathParam("categoryName") @WebParam(name = "categoryName") String categoryName,
			@PathParam("parentCategoryName") @WebParam(name = "parentCategoryName") String parentCategoryName)
			throws CategoryExistsException, CategoryParentNotFoundException {
		return categoryService.addCategory(categoryName, parentCategoryName);
	}

	@POST
	@Path("/modify/categoryName/{categoryName}/oldParentId/{oldParentId}/newParentId/{newParentId}")
	@WebMethod(operationName = "modifyCategory")
	public CategoryTO modifyCategory(
			@PathParam("categoryName") @WebParam(name = "categoryName") String categoryName,
			@PathParam("oldParentId") @WebParam(name = "oldParentId") int oldParentId,
			@PathParam("newParentId") @WebParam(name = "newParentId") int newParentId)
			throws CategoryNotFoundException, CategoryParentNotFoundException {
		return categoryService.modifyCategory(categoryName, oldParentId,
				newParentId);
	}

	@DELETE
	@Path("/delete/categoryId/{categoryId}")
	@WebMethod(operationName = "removeCategoryWithId")
	public boolean removeCategory(
			@PathParam("categoryId") @WebParam(name = "categoryId") int categoryId)
			throws CategoryNotFoundException {
		return categoryService.removeCategory(categoryId);
	}

	@DELETE
	@Path("/delete/categoryName/{categoryName}")
	@WebMethod(operationName = "removeCategoryWithName")
	public boolean removeCategory(@PathParam("categoryName") @WebParam(name = "categoryName") String categoryName)
			throws CategoryNotFoundException {
		return categoryService.removeCategory(categoryName);
	}
}
